<?php

namespace app\admin\controller;

use app\admin\model\LogModel;
use app\admin\model\OrderInModel;
use app\admin\model\ShopModel;

class Script extends \app\BaseController
{
    public function index(){
        $p=$this->request->get();
        $orderInfo=OrderInModel::find($p['id']);
        $shopInf=ShopModel::where([['shop_id','=',$orderInfo->shop_id]])->find();
        api_return(200,'',[
            'orderInfo'=>$orderInfo,
            'shopInfo'=>$shopInf
        ]);
    }
    public static function test(){
        set_time_limit(-1);
        $orderList=OrderInModel::getList([
            'where'=>[
//                ['order_refund_info','like','%complaintFlowList%'],
                ['order_refund_info','not in',['[]','{}']]
//                ['id','=',90752773793275],
            ],
            'page'=>'all'
        ]);
        foreach ($orderList as $item){
            echo $item->id."\n";
            if(!empty($item->order_refund_info['applyTime'])) {
                $item->refund_apply_time = date('Y-m-d H:i:s', strtotime($item->order_refund_info['applyTime']));
                $item->save();
            }
        }
    }
    public function orderDetail(){
        $p=$this->request->get();
        $orderInfo=OrderInModel::find($p['id']);
        $shopInfo=ShopModel::where([['shop_id','=',$orderInfo->shop_id]])->find();
        $url='https://api.baidu.com/json/sms/service/PlatOrderBusinessQueryService/getOrderDetail';
        $params=[
            'header'=>[
                'userName'=>$shopInfo->username,
                'accessToken'=>$shopInfo->accesstoken['accessToken'],
            ],
            'body'=>[
                'orderId'=>$orderInfo->id,
                'ucid'=>$orderInfo->ucid,
                'shopId'=>$orderInfo->shop_id,
                'appId'=>5
            ]
        ];
        $header=[
            'Content-Type: application/json'
        ];
        $res=json_decode(post($url,json_encode($params),$header),true);
        return json($res);
    }

    /**
     * 为2023.04.01以后的售后订单添加refundType
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function refundType(){

        $where=[
            ['create_time','>','2023-04-01'],
            ['refund_type','=',0]
        ];
        while ($info=OrderInModel::where($where)->field('id,order_refund_info')->whereRaw('order_refund_info is not null')->find())
        {
            $info->refund_type=$info->order_refund_info['refundType'];
            $info->save();
            echo $info->id.'-'.$info->order_refund_info['refundType']."\n";
        }
    }
}
